package com.lv.pc.viewpagerphotocarousel;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity implements ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private ViewGroup viewGroup;
    private MyViewPagerAdapter viewPagerAdapter;
    private List<View> pagerList;
    private ViewPagerThread viewPagerThread=null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpage);
        viewGroup = (ViewGroup) findViewById(R.id.ico_layout);

        pagerList = new ArrayList<View>();
        for (int i = 0; i < 5; i++) {
            View v_content = getLayoutInflater().inflate(R.layout.content_layout, null);
            setContentBackground(v_content, i);
            ((TextView) v_content.findViewById(R.id.textview)).setText("这是第" + i + "个item");
            pagerList.add(v_content);
            View v_ico = getLayoutInflater().inflate(R.layout.ico_layout, null);
            ((TextView) v_ico.findViewById(R.id.textview)).setText("" + i);
            viewGroup.addView(v_ico);
        }
        setIcoBackground(0);
        viewPagerAdapter = new MyViewPagerAdapter(pagerList);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOnPageChangeListener(this);
        Log.v("lvzhongyi",viewPager.getChildCount()+"child");
        if (viewPagerThread==null){
            viewPagerThread = new ViewPagerThread(viewPager,0,viewPagerAdapter.getCount(),MainActivity.this) ;
            viewPagerThread.start();
        }
    }

    public void setContentBackground(View view, int position) {
        if (position % 2 == 0) {
            view.setBackgroundResource(R.color.color_two);
        } else {
            view.setBackgroundResource(R.color.color_one);
        }
    }

    public void setIcoBackground(int position) {
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            ((View) viewGroup.getChildAt(i)).setOnClickListener(new IcoOnClick(i));
            TextView textView = (TextView) ((View) viewGroup.getChildAt(i)).findViewById(R.id.textview);
            if (i == position) {
                textView.setBackgroundResource(R.drawable.ico_select);
            } else {
                textView.setBackgroundResource(R.drawable.ico);
            }
        }
    }

    private class IcoOnClick implements View.OnClickListener {
        private int position;

        public IcoOnClick(int position) {
            this.position = position;
        }

        public void onClick(View v) {
            viewPager.setCurrentItem(position);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.d("lvzhongyi","onPageScrolled");
    }

    @Override
    public void onPageSelected(int position) {
        setIcoBackground(position);
        viewPagerThread.setIndex(position);
        Log.d("lvzhongyi","onPageSelected");
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        Log.d("lvzhongyi","onPageScrollStateChanged");
    }
}
